package Items.Market;

import java.text.SimpleDateFormat;
import java.util.*;

public class Market {
    private String name="家乐福商场";
    private String address="屯溪路222号";

    private List<Vip> list= new ArrayList<>();
    private List<Gift>gift=new ArrayList<>();

    public Market(){
        list.add(new Vip(313442,"张三","13515671412",300,"zs123456","2019-1-3"));
        list.add(new Vip(241245,"李四","13112157842",300,"ls123456","2017-4-1"));
        gift.add(new Gift("抽纸一包",50));
        gift.add(new Gift("牙刷一支",100));
        gift.add(new Gift("手动剃须刀",300));
        gift.add(new Gift("电动剃须刀",800));
        gift.add(new Gift("鸡蛋一个",200));
    }


    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public void showMarket(){
        System.out.println(getName());
        System.out.println(getAddress());
    }

    public boolean addVip(Vip v1){
        Vip vip=new Vip();
        int vipNum=(int)(Math.random()*(1000+9999));
        v1.setVipNum(vipNum);
        Iterator<Vip> ir=list.iterator();
        while (ir.hasNext()){
            vip=ir.next();
            if (vip.getVipNum()==vipNum){
                addVip(v1);
            }
        }
        /*Date date = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd").format(date.getTime());*/
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date d=new Date();
        String str=sdf.format(d);
        v1.setCreatDate(str);
        System.out.println("恭喜，开通会员卡成功，系统赠送您500积分！");
        v1.setIntegral(500);
        list.add(v1);
        System.out.println("卡号："+v1.getVipNum()+"\t姓名："+v1.getName()+"\t初始积分："+v1.getIntegral()+"\t开卡时间:"+v1.getCreatDate());
        return true;
    }

    public boolean addIntegral(int vipNum,String pwd,int integral){//累加积分
        Vip vip=new Vip();
        Iterator<Vip> ir=list.iterator();
        while (ir.hasNext()){
            vip=ir.next();
            if (vip.getVipNum()==vipNum&&vip.getPwd().equals(pwd)){
                System.out.println("上次积分");
                System.out.println("卡号："+vip.getVipNum()+"\t姓名："+vip.getName()+"\t积分："+vip.getIntegral()+"\t"+vip.getPhone());
                System.out.println("本次积分");
                vip.setIntegral(vip.getIntegral()+integral);
                System.out.println("卡号："+vip.getVipNum()+"\t姓名："+vip.getName()+"\t积分："+vip.getIntegral()+"\t"+vip.getPhone());
                return true;
            }
        }
        System.out.println("密码或卡号错误");
        return false;
    }

    public boolean checkIntegral(int vipNum,String vipPwd){
        Iterator<Vip> ir=list.iterator();
        while (ir.hasNext()){
            Vip v=ir.next();
            if (v.getVipNum()==(vipNum)&& v.getPwd().equals(vipPwd)){
                System.out.println("姓名\t\t电话\t\t剩余积分");
                System.out.println(v.getName()+" "+v.getPhone()+" "+v.getIntegral());
                return true;
            }
        }
        return false;
    }

    public boolean modify(int vipNum,String pwd,String newPwd){
        Iterator<Vip> ir = list.iterator();
        while (ir.hasNext()){
            Vip v=ir.next();
            if (v.getVipNum()==vipNum&&v.getPwd().equals(pwd)){
                v.setPwd(newPwd);
                return true;
            }
        }
        return false;
    }

    public boolean exchange(int vipNum,String pwd){
        Iterator<Vip> ir = list.iterator();
        while (ir.hasNext()){
            Vip v = ir.next();
            if (v.getVipNum() == vipNum&&v.getPwd() == pwd){
                Iterator<Gift> ig = gift.iterator();
                while (ig.hasNext()){
                    Gift gift1 = ig.next();
                    if (gift1.getName().equals(name)){
                        if (v.getIntegral()-gift1.getIntegral()>0){
                            v.setIntegral(v.getIntegral()- gift1.getIntegral());
                            gift.remove(gift);
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
    /*@Override
    public String toString() {
        SimpleDateFormat sd=new SimpleDateFormat();
        sd.applyPattern("yyyy年MM月dd日");
        Iterator<Vip> iterator=list.iterator();
        String b="";
        while (iterator.hasNext()){
            Vip v=iterator.next();
            String a=sd.format(v.getCreatDate());
            b=v.getVipNum()+"\t"+v.getName()+"\t"+v.getName()+"\t"+v.getPwd()+"\t"+v.getPhone()+"\t"+v.getIntegral()+"\t"+v.getCreatDate();
        }
        return b;
    }*/
}
